package com.un.ebs.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.un.ebs.core.config.OSSConfig;
import com.un.ebs.core.util.OSSUtil;
import com.un.ebs.product.domain.ProductImage;
import com.un.ebs.product.enums.ProductImageTypeEnum;
import com.un.ebs.product.mapper.ProductImageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import com.un.ebs.product.service.ProductImageService;
import com.un.ebs.product.dto.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LIUBO
 * @since 2019-11-03
 */
@RestController
@RequestMapping("/productImage")
@Api(value = "product-image", description = "商品图片")
public class ProductImageController {

    @Autowired
    private ProductImageService service;

    @Autowired
    private ProductImageMapper productImageMapper;

    @PostMapping(value = "upload",produces = {MediaType.APPLICATION_JSON_VALUE})
    @ApiOperation(value = "upload", notes = "上传图片")
    public WebResult upload(@RequestParam(value = "file")MultipartFile file,
                            @RequestParam(value = "productId") String productId,
                            @RequestParam(required = false) ProductImageTypeEnum imgType) {
        if(imgType==null){
            imgType=ProductImageTypeEnum.Main;
        }
        return WebUtils.success(service.uploadOss(file, productId,imgType));
    }

    @GetMapping(value = "getUrl")
    @ApiOperation(value = "getUrl", notes = "获取图片路径")
    public WebResult getUrl(@RequestParam(value = "key") String key){
        return WebUtils.success(OSSUtil.getObjectUrl(key));
    }

    @DeleteMapping("delete/{id}")
    @ApiOperation(value = "delete", notes = "删除")
    public WebResult delete(@PathVariable("id") String id) {
        service.delete(id);
        return WebUtils.success();
    }

    @DeleteMapping("deleteByProduct/{productId}")
    @ApiOperation(value = "deleteByProduct", notes = "删除")
    public WebResult deleteByProduct(@PathVariable("productId") String productId) {
        service.deleteByProductId(productId);
        return WebUtils.success();
    }

    @GetMapping("/{productId}")
    @ApiOperation(value = "list", notes = "查询商品的所有图片")
    public WebResult list(@PathVariable("productId") String productId) {
        return WebUtils.success(service.queryBySpuId(productId));
    }

    @GetMapping("view/{productId}")
    @ApiOperation(value = "listView", notes = "查询商品的所有图片，不需要权限认证")
    public WebResult listView(@PathVariable String productId) {
        return list(productId);
    }

    @PostMapping("changeSort")
    @ApiOperation(value = "changeSort", notes = "修改 ProductImage 的序号")
    public WebResult changeSort(@RequestParam String id, @RequestParam int index) {
        service.changeSort(id, index);
        return WebUtils.success();
    }

}